package com.yonghui.springsecurityoauth2demo.domain;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 
 * @TableName oauth_client_details
 */
public class OauthClientDetails implements Serializable, ClientDetails {
    /**
     * 
     */
    private String clientId;

    /**
     * 
     */
    private String resourceIds;

    /**
     * 
     */
    private String clientSecret;

    /**
     * 
     */
    private String scope;

    /**
     * 
     */
    private String authorizedGrantTypes;

    /**
     * 
     */
    private String webServerRedirectUri;

    /**
     * 
     */
    private String authorities2;

    /**
     * 
     */
    private Integer accessTokenValidity;

    /**
     * 
     */
    private Integer refreshTokenValidity;

    /**
     * 
     */
    private String additionalInformation;

    /**
     * 
     */
    private String autoapprove;

    private static final long serialVersionUID = 1L;

    public String getAuthorities2() {
        return authorities2;
    }

    public void setAuthorities2(String authorities2) {
        this.authorities2 = authorities2;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public void setResourceIds(String resourceIds) {
        this.resourceIds = resourceIds;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public void setAuthorizedGrantTypes(String authorizedGrantTypes) {
        this.authorizedGrantTypes = authorizedGrantTypes;
    }

    public String getWebServerRedirectUri() {
        return webServerRedirectUri;
    }

    public void setWebServerRedirectUri(String webServerRedirectUri) {
        this.webServerRedirectUri = webServerRedirectUri;
    }



    public Integer getAccessTokenValidity() {
        return accessTokenValidity;
    }

    public void setAccessTokenValidity(Integer accessTokenValidity) {
        this.accessTokenValidity = accessTokenValidity;
    }

    public Integer getRefreshTokenValidity() {
        return refreshTokenValidity;
    }

    public void setRefreshTokenValidity(Integer refreshTokenValidity) {
        this.refreshTokenValidity = refreshTokenValidity;
    }

    public void setAdditionalInformation(String additionalInformation) {
        this.additionalInformation = additionalInformation;
    }

    public String getAutoapprove() {
        return autoapprove;
    }

    public void setAutoapprove(String autoapprove) {
        this.autoapprove = autoapprove;
    }

    @Override
    public String getClientId() {
        return clientId;
    }

    @Override
    public Set<String> getResourceIds() {
        return null;
    }

    @Override
    public boolean isSecretRequired() {
        return false;
    }

    @Override
    public String getClientSecret() {
        return clientSecret;
    }

    @Override
    public boolean isScoped() {
        return false;
    }

    @Override
    public Set<String> getScope() {
        Set<String> set = new HashSet<>();
        //set.add("all");
        String[] split = scope.split(",");
        for(String s : split){
            set.add(s);
        }
        return set;
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        Set<String> set = new HashSet<>();
        String[] split = authorizedGrantTypes.split(",");
        for(String s : split){
            set.add(s);
        }
        return set;
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        Set<String> set = new HashSet<>();
        //set.add("http://www.freeze721.com/success");
        String[] split = webServerRedirectUri.split(",");
        for(String s : split){
            set.add(s);
        }
        return set;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list2 = new ArrayList<>();
        GrantedAuthority s = new GrantedAuthority() {
            @Override
            public String getAuthority() {
                return "ROLE_CLIENT";
            }
        };
        list2.add(s);
        //List<String> list = Arrays.asList("ROLE_CLIENT");
        return list2;
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return accessTokenValidity;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return refreshTokenValidity;
    }

    @Override
    public boolean isAutoApprove(String s) {
        boolean bool = Boolean.valueOf(autoapprove);
        return bool;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return null;
    }
}